import json

from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.models import GoodsInfo


class Mygoods(View):
    """我的发布"""

    def get(self, request):
        author = request.GET.get('author')
        print(author)
        name_author = GoodsInfo.objects.filter(name=author)
        lists = []
        for goods in name_author:
            print(goods.name)
            print(goods.goodsName)
            time = str(goods.create_time)
            time1 = time[0:11] + time[11:16]
            mygoods_dict = {
                "goodsId": goods.id,
                "goodsName": goods.goodsName,
                "goodsDesc": goods.goodsDesc,
                "newPrice": goods.newPrice,
                "oldPrice": goods.oldPrice,
                "goodsAuthorName": goods.name,
                "createTime": time1,

            }
            lists.append(mygoods_dict)

        return http.JsonResponse({'errmsg': '我的发布显示成功', 'success': '操作成功','item': lists})

    def post(self, request):
        json_dict = json.loads(request.body.decode())
        authorName = json_dict.get('authorName')
        id = json_dict.get('id')
        print(authorName, id)

        name = GoodsInfo.objects.filter(name=authorName)
        if name:
            GoodsInfo.objects.filter(id=id).delete()

        return http.JsonResponse({'errmsg': '删除成功', 'success': '操作成功', 'item': 1})